package Util;

import dao.NingboGDPMapper;
import domain.NingboGDP;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class ReadningboGDP {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static {
        try {
            reader = Resources.getResourceAsReader("Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession() {
        return sqlSessionFactory;
    }

    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        ArrayList<List> a = new ArrayList();
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                if (tempString.split("\t") != null)
                    a.add(Arrays.asList(tempString.split("\t")));
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            System.out.println(a.toString());
            reader.close();
            ArrayList<NingboGDP> ningboGDPSArray = new ArrayList<NingboGDP>();
            String county[] = new String[]{"a", "Total", "Urban", "Haishu", "Jiangbei", "Zhenhai", "Beilun", "Yinzhou", "Fenghua", "Yuyao", "Cixi", "Xinghai", "Xiangshan"};
            for (int i = 3; i < a.get(0).size(); i++) {

                NingboGDP nbGDp = new NingboGDP();
                nbGDp.setId(i - 2);
                nbGDp.setCounty(county[i]);
                for (int j = 1; j < a.size(); j++) {
                    int num = Integer.parseInt((String) a.get(j).get(i));
                    if (j == 1) {
                        nbGDp.setTotal(num);
                    }
                    if (j == 2) {
                        nbGDp.setPrimaryIndustry(num);
                    }
                    if (j == 3) {
                        nbGDp.setSecondaryIndustry(num);
                    }
                    if (j == 4) {
                        nbGDp.setTertianIndustn(num);
                    }
                    if (j == 5) {
                        nbGDp.setAgriculture(num);
                    }
                    if (j == 6) {
                        nbGDp.setIndustiy(num);
                    }
                    if (j == 7) {
                        nbGDp.setConstructions(num);
                    }
                    if (j == 8) {
                        nbGDp.setRetail(num);
                    }
                    if (j == 9) {
                        nbGDp.setTransportation(num);
                    }
                    if (j == 10) {
                        nbGDp.setHoteling(num);
                    }
                    if (j == 11) {
                        nbGDp.setSoftware(num);
                    }
                    if (j == 12) {
                        nbGDp.setFinancialIndustiy(num);
                    }
                    if (j == 13) {
                        nbGDp.setRealEstateIndustiy(num);
                    }
                    if (j == 14) {
                        nbGDp.setLeasehold(num);
                    }
                    if (j == 15) {
                        nbGDp.setScientificResearch(num);
                    }
                    if (j == 16) {
                        nbGDp.setWater(num);
                    }
                    if (j == 17) {
                        nbGDp.setResidentialSenice(num);
                    }
                    if (j == 18) {
                        nbGDp.setEducation(num);
                    }
                    if (j == 19) {
                        nbGDp.setHealthCare(num);
                    }
                    if (j == 20) {
                        nbGDp.setCultureSports(num);
                    }
                    if (j == 21) {
                        nbGDp.setPublicManagement(num);
                    }
                }
                SqlSession session = sqlSessionFactory.openSession();
                NingboGDPMapper userOperation = session
                        .getMapper(NingboGDPMapper.class);
                System.out.println(userOperation.insert(nbGDp));
                session.commit();
                session.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public static void main(String[] args) {
        String fileName = "C:\\Users\\admin\\Desktop\\aa.txt";
        ReadningboGDP.readFileByLines(fileName);
    }
}
